package cmj.cloudmusic.fragment.Friends;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import cmj.cloudmusic.R;
import cmj.cloudmusic.adapter.TabFragmentAdapter;

/**
 * Created by 王虎 on 2016/12/10.
 */
public class FriendsFragment extends Fragment {
    private View view;

    private TabLayout tabLayout;
    private List<String> tabTitles;

    private ViewPager viewPager;
    private List<Fragment> fragments;
    private FragmentPagerAdapter adapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null) {
                parent.removeView(view);
            }
            return view;
        }
        view=inflater.inflate(R.layout.friends_fragment,null);
        tabLayout= (TabLayout) view.findViewById(R.id.tablayout);
        viewPager= (ViewPager) view.findViewById(R.id.viewpager);

        initTab();
        initVP();
        return view;
    }

    private void initTab(){


        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        tabTitles=new ArrayList<>();
        tabTitles.add("动态");
        tabTitles.add("附近");
        tabTitles.add("好友");

        tabLayout.addTab(tabLayout.newTab().setText("动态"), true);
        tabLayout.addTab(tabLayout.newTab().setText("附近"));
        tabLayout.addTab(tabLayout.newTab().setText("好友"));
    }
    private void initVP(){
        F_dongtaiFragment f_dongtaiFragment=new F_dongtaiFragment();
        F_fujinFragment f_fujinFragment=new F_fujinFragment();
        F_friendsFragment f_friendsFragment=new F_friendsFragment();

        fragments=new ArrayList<>();
        fragments.add(f_dongtaiFragment);
        fragments.add(f_fujinFragment);
        fragments.add(f_friendsFragment);
        adapter=new TabFragmentAdapter(getChildFragmentManager(),fragments,tabTitles);

        viewPager.setAdapter(adapter);

        tabLayout.setupWithViewPager(viewPager);
        Log.e("friendfragment", "initVP: ==============="+fragments.size() );
    }
}
